using DA.AnimGraph;
using System;

namespace DA.AnimGraph.Editor {
    internal abstract class AGAPerk_PerkBase {
        public AUID uuid;

        public abstract AGAPerkModelBase modelProperty { get; protected set; }

        public AGAPerk_PerkBase(AGAPerkModelBase model) {
            uuid = AUIDGenerator.Gen();
            modelProperty = model;
        }

        /// <summary>
        /// 返回null则不导出数据
        /// </summary>
        public abstract PublishPerk Publish(AGAPublisher.PublishContext context);

        /// <summary>
        /// 删除时需要调用处理一些事情
        /// </summary>
        public virtual void OnDelete(AGEContext context) { }
    }

    internal class AGAPerk_Timeline : AGAPerk_PerkBase {
        public override AGAPerkModelBase modelProperty { get => model; protected set => model = (AGAPerkModel_Timeline)value; }
        public AGAPerkModel_Timeline model;

        public AGAPerk_Timeline(AGAPerkModelBase model) : base(model) {
            this.model.timelineUuid = uuid;
        }

        public override PublishPerk Publish(AGAPublisher.PublishContext context) {
            return new TimelinePPerk() {
                uuid = uuid,
                data = model.Publish(context)
            };
        }
        public override void OnDelete(AGEContext context) {
            context.timelineManager.RemoveTimeline(model.timelineUuid);
        }
    }

    [ModelOf(typeof(AGAPerk_Timeline))]
    internal class AGAPerkModel_Timeline : AGAPerkModelBase {
        public AUID timelineUuid;

        internal TimelinePerk.SetupData Publish(AGAPublisher.PublishContext context) {
            return new TimelinePerk.SetupData() {
                timelineUuid = timelineUuid,
            };
        }
    }

    // ========================================

    internal class AGAPerk_SyncGroup : AGAPerk_PerkBase {
        public override AGAPerkModelBase modelProperty { get => model; protected set => model = (AGAPerkModel_SyncGroup)value; }
        public AGAPerkModel_SyncGroup model;

        public AGAPerk_SyncGroup(AGAPerkModelBase model) : base(model) {
        }

        public override PublishPerk Publish(AGAPublisher.PublishContext context) {
            // 如果没填 groupName 则不导出
            var data = model.Publish(context);
            if (data == null) return null;
            return new SyncGroupPPerk() {
                uuid = uuid,
                data = (SyncGroupPerk.SetupData)data
            };
        }
    }

    [ModelOf(typeof(AGAPerk_SyncGroup))]
    internal class AGAPerkModel_SyncGroup : AGAPerkModelBase {
        public string groupName;

        internal SyncGroupPerk.SetupData? Publish(AGAPublisher.PublishContext context) {
            if (string.IsNullOrEmpty(groupName)) return null;
            return new SyncGroupPerk.SetupData() {
                groupID = groupName,
            };
        }
    }

    // ========================================

    internal class AGAPerk_CurveSampler : AGAPerk_PerkBase {
        public override AGAPerkModelBase modelProperty { get => throw new NotImplementedException(); protected set => throw new NotImplementedException(); }
        public CurveSamplerPerk.SetupData data;

        public AGAPerk_CurveSampler(AGAPerkModelBase model) : base(model) {
        }

        public override PublishPerk Publish(AGAPublisher.PublishContext context) {
            return new CurveSamplerPPerk() {
                uuid = uuid,
                data = data
            };
        }
    }
}
